{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.11 \u8bfb\u5199\u4e8c\u8fdb\u5236\u6570\u7ec4\u6570\u636e\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8bfb\u5199\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6570\u7ec4\u7684\u7ed3\u6784\u5316\u6570\u636e\u5230Python\u5143\u7ec4\u4e2d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u4f7f\u7528 struct \u6a21\u5757\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u3002\n\u4e0b\u9762\u662f\u4e00\u6bb5\u793a\u4f8b\u4ee3\u7801\u5c06\u4e00\u4e2aPython\u5143\u7ec4\u5217\u8868\u5199\u5165\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 struct \u5c06\u6bcf\u4e2a\u5143\u7ec4\u7f16\u7801\u4e3a\u4e00\u4e2a\u7ed3\u6784\u4f53\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from struct import Struct\ndef write_records(records, format, f):\n    '''\n    Write a sequence of tuples to a binary file of structures.\n    '''\n    record_struct = Struct(format)\n    for r in records:\n        f.write(record_struct.pack(*r))\n\n# Example\nif __name__ == '__main__':\n    records = [ (1, 2.3, 4.5),\n                (6, 7.8, 9.0),\n                (12, 13.4, 56.7) ]\n    with open('data.b', 'wb') as f:\n        write_records(records, '<idd', f)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\u6765\u8bfb\u53d6\u8fd9\u4e2a\u6587\u4ef6\u5e76\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\u5217\u8868\u3002\n\u9996\u5148\uff0c\u5982\u679c\u4f60\u6253\u7b97\u4ee5\u5757\u7684\u5f62\u5f0f\u589e\u91cf\u8bfb\u53d6\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from struct import Struct\n\ndef read_records(format, f):\n    record_struct = Struct(format)\n    chunks = iter(lambda: f.read(record_struct.size), b'')\n    return (record_struct.unpack(chunk) for chunk in chunks)\n\n# Example\nif __name__ == '__main__':\n    with open('data.b','rb') as f:\n        for rec in read_records('<idd', f):\n            # Process rec\n            ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u5c06\u6574\u4e2a\u6587\u4ef6\u4e00\u6b21\u6027\u8bfb\u53d6\u5230\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\u4e2d\uff0c\u7136\u540e\u5728\u5206\u7247\u89e3\u6790\u3002\u90a3\u4e48\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from struct import Struct\n\ndef unpack_records(format, data):\n    record_struct = Struct(format)\n    return (record_struct.unpack_from(data, offset)\n            for offset in range(0, len(data), record_struct.size))\n\n# Example\nif __name__ == '__main__':\n    with open('data.b', 'rb') as f:\n        data = f.read()\n    for rec in unpack_records('<idd', data):\n        # Process rec\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e24\u79cd\u60c5\u51b5\u4e0b\u7684\u7ed3\u679c\u90fd\u662f\u4e00\u4e2a\u53ef\u8fd4\u56de\u7528\u6765\u521b\u5efa\u8be5\u6587\u4ef6\u7684\u539f\u59cb\u5143\u7ec4\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u9700\u8981\u7f16\u7801\u548c\u89e3\u7801\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u7a0b\u5e8f\u800c\u8a00\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528 struct \u6a21\u5757\u3002\n\u4e3a\u4e86\u58f0\u660e\u4e00\u4e2a\u65b0\u7684\u7ed3\u6784\u4f53\uff0c\u53ea\u9700\u8981\u50cf\u8fd9\u6837\u521b\u5efa\u4e00\u4e2a Struct \u5b9e\u4f8b\u5373\u53ef\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Little endian 32-bit integer, two double precision floats\nrecord_struct = Struct('<idd')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ed3\u6784\u4f53\u901a\u5e38\u4f1a\u4f7f\u7528\u4e00\u4e9b\u7ed3\u6784\u7801\u503ci, d, f\u7b49\n[\u53c2\u8003 Python\u6587\u6863 ]\u3002\n\u8fd9\u4e9b\u4ee3\u7801\u5206\u522b\u4ee3\u8868\u67d0\u4e2a\u7279\u5b9a\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u7c7b\u578b\u598232\u4f4d\u6574\u6570\uff0c64\u4f4d\u6d6e\u70b9\u6570\uff0c32\u4f4d\u6d6e\u70b9\u6570\u7b49\u3002\n\u7b2c\u4e00\u4e2a\u5b57\u7b26 < \u6307\u5b9a\u4e86\u5b57\u8282\u987a\u5e8f\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b83\u8868\u793a\u201d\u4f4e\u4f4d\u5728\u524d\u201d\u3002\n\u66f4\u6539\u8fd9\u4e2a\u5b57\u7b26\u4e3a > \u8868\u793a\u9ad8\u4f4d\u5728\u524d\uff0c\u6216\u8005\u662f ! \u8868\u793a\u7f51\u7edc\u5b57\u8282\u987a\u5e8f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ea7\u751f\u7684 Struct \u5b9e\u4f8b\u6709\u5f88\u591a\u5c5e\u6027\u548c\u65b9\u6cd5\u7528\u6765\u64cd\u4f5c\u76f8\u5e94\u7c7b\u578b\u7684\u7ed3\u6784\u3002\nsize \u5c5e\u6027\u5305\u542b\u4e86\u7ed3\u6784\u7684\u5b57\u8282\u6570\uff0c\u8fd9\u5728I/O\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\u3002\npack() \u548c unpack() \u65b9\u6cd5\u88ab\u7528\u6765\u6253\u5305\u548c\u89e3\u5305\u6570\u636e\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from struct import Struct\nrecord_struct = Struct('<idd')\nrecord_struct.size"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "record_struct.pack(1, 2.0, 3.0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "record_struct.unpack(_)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u65f6\u5019\u4f60\u8fd8\u4f1a\u770b\u5230 pack() \u548c unpack() \u64cd\u4f5c\u4ee5\u6a21\u5757\u7ea7\u522b\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import struct\nstruct.pack('<idd', 1, 2.0, 3.0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "struct.unpack('<idd', _)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u611f\u89c9\u6ca1\u6709\u5b9e\u4f8b\u65b9\u6cd5\u90a3\u4e48\u4f18\u96c5\uff0c\u7279\u522b\u662f\u5728\u4f60\u4ee3\u7801\u4e2d\u540c\u6837\u7684\u7ed3\u6784\u51fa\u73b0\u5728\u591a\u4e2a\u5730\u65b9\u7684\u65f6\u5019\u3002\n\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a Struct \u5b9e\u4f8b\uff0c\u683c\u5f0f\u4ee3\u7801\u53ea\u4f1a\u6307\u5b9a\u4e00\u6b21\u5e76\u4e14\u6240\u6709\u7684\u64cd\u4f5c\u88ab\u96c6\u4e2d\u5904\u7406\u3002\n\u8fd9\u6837\u4e00\u6765\u4ee3\u7801\u7ef4\u62a4\u5c31\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u4e86(\u56e0\u4e3a\u4f60\u53ea\u9700\u8981\u6539\u53d8\u4e00\u5904\u4ee3\u7801\u5373\u53ef)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8bfb\u53d6\u4e8c\u8fdb\u5236\u7ed3\u6784\u7684\u4ee3\u7801\u8981\u7528\u5230\u4e00\u4e9b\u975e\u5e38\u6709\u8da3\u800c\u4f18\u7f8e\u7684\u7f16\u7a0b\u6280\u5de7\u3002\n\u5728\u51fd\u6570\u3000read_records \u4e2d\uff0citer() \u88ab\u7528\u6765\u521b\u5efa\u4e00\u4e2a\u8fd4\u56de\u56fa\u5b9a\u5927\u5c0f\u6570\u636e\u5757\u7684\u8fed\u4ee3\u5668\uff0c\u53c2\u80035.8\u5c0f\u8282\u3002\n\u8fd9\u4e2a\u8fed\u4ee3\u5668\u4f1a\u4e0d\u65ad\u7684\u8c03\u7528\u4e00\u4e2a\u7528\u6237\u63d0\u4f9b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61(\u6bd4\u5982 lambda: f.read(record_struct.size) )\uff0c\n\u76f4\u5230\u5b83\u8fd4\u56de\u4e00\u4e2a\u7279\u6b8a\u7684\u503c(\u5982b\u2019\u2018)\uff0c\u8fd9\u65f6\u5019\u8fed\u4ee3\u505c\u6b62\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('data.b', 'rb')\nchunks = iter(lambda: f.read(20), b'')\nchunks"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for chk in chunks:\nprint(chk)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u4f60\u6240\u89c1\uff0c\u521b\u5efa\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u4e00\u4e2a\u539f\u56e0\u662f\u5b83\u80fd\u5141\u8bb8\u4f7f\u7528\u4e00\u4e2a\u751f\u6210\u5668\u63a8\u5bfc\u6765\u521b\u5efa\u8bb0\u5f55\u3002\n\u5982\u679c\u4f60\u4e0d\u4f7f\u7528\u8fd9\u79cd\u6280\u672f\uff0c\u90a3\u4e48\u4ee3\u7801\u53ef\u80fd\u4f1a\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def read_records(format, f):\n    record_struct = Struct(format)\n    while True:\n        chk = f.read(record_struct.size)\n        if chk == b'':\n            break\n        yield record_struct.unpack(chk)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u51fd\u6570 unpack_records() \u4e2d\u4f7f\u7528\u4e86\u53e6\u5916\u4e00\u79cd\u65b9\u6cd5 unpack_from() \u3002\nunpack_from() \u5bf9\u4e8e\u4ece\u4e00\u4e2a\u5927\u578b\u4e8c\u8fdb\u5236\u6570\u7ec4\u4e2d\u63d0\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u975e\u5e38\u6709\u7528\uff0c\n\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u4ea7\u751f\u4efb\u4f55\u7684\u4e34\u65f6\u5bf9\u8c61\u6216\u8005\u8fdb\u884c\u5185\u5b58\u590d\u5236\u64cd\u4f5c\u3002\n\u4f60\u53ea\u9700\u8981\u7ed9\u5b83\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32(\u6216\u6570\u7ec4)\u548c\u4e00\u4e2a\u5b57\u8282\u504f\u79fb\u91cf\uff0c\u5b83\u4f1a\u4ece\u90a3\u4e2a\u4f4d\u7f6e\u5f00\u59cb\u76f4\u63a5\u89e3\u5305\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u4f7f\u7528 unpack() \u6765\u4ee3\u66ff unpack_from() \uff0c\n\u4f60\u9700\u8981\u4fee\u6539\u4ee3\u7801\u6765\u6784\u9020\u5927\u91cf\u7684\u5c0f\u7684\u5207\u7247\u4ee5\u53ca\u8fdb\u884c\u504f\u79fb\u91cf\u7684\u8ba1\u7b97\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def unpack_records(format, data):\n    record_struct = Struct(format)\n    return (record_struct.unpack(data[offset:offset + record_struct.size])\n            for offset in range(0, len(data), record_struct.size))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u65b9\u6848\u9664\u4e86\u4ee3\u7801\u770b\u4e0a\u53bb\u5f88\u590d\u6742\u5916\uff0c\u8fd8\u5f97\u505a\u5f88\u591a\u989d\u5916\u7684\u5de5\u4f5c\uff0c\u56e0\u4e3a\u5b83\u6267\u884c\u4e86\u5927\u91cf\u7684\u504f\u79fb\u91cf\u8ba1\u7b97\uff0c\n\u590d\u5236\u6570\u636e\u4ee5\u53ca\u6784\u9020\u5c0f\u7684\u5207\u7247\u5bf9\u8c61\u3002\n\u5982\u679c\u4f60\u51c6\u5907\u4ece\u8bfb\u53d6\u5230\u7684\u4e00\u4e2a\u5927\u578b\u5b57\u8282\u5b57\u7b26\u4e32\u4e2d\u89e3\u5305\u5927\u91cf\u7684\u7ed3\u6784\u4f53\u7684\u8bdd\uff0cunpack_from() \u4f1a\u8868\u73b0\u7684\u66f4\u51fa\u8272\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u89e3\u5305\u7684\u65f6\u5019\uff0ccollections \u6a21\u5757\u4e2d\u7684\u547d\u540d\u5143\u7ec4\u5bf9\u8c61\u6216\u8bb8\u662f\u4f60\u60f3\u8981\u7528\u5230\u7684\u3002\n\u5b83\u53ef\u4ee5\u8ba9\u4f60\u7ed9\u8fd4\u56de\u5143\u7ec4\u8bbe\u7f6e\u5c5e\u6027\u540d\u79f0\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from collections import namedtuple\n\nRecord = namedtuple('Record', ['kind','x','y'])\n\nwith open('data.p', 'rb') as f:\n    records = (Record(*r) for r in read_records('<idd', f))\n\nfor r in records:\n    print(r.kind, r.x, r.y)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u5904\u7406\u5927\u91cf\u7684\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4f60\u6700\u597d\u4f7f\u7528 numpy \u6a21\u5757\u3002\n\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c06\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6570\u636e\u8bfb\u53d6\u5230\u4e00\u4e2a\u7ed3\u6784\u5316\u6570\u7ec4\u4e2d\u800c\u4e0d\u662f\u4e00\u4e2a\u5143\u7ec4\u5217\u8868\u4e2d\u3002\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\nf = open('data.b', 'rb')\nrecords = np.fromfile(f, dtype='<i,<d,<d')\nrecords"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "records[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "records[1]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u63d0\u4e00\u70b9\uff0c\u5982\u679c\u4f60\u9700\u8981\u4ece\u5df2\u77e5\u7684\u6587\u4ef6\u683c\u5f0f(\u5982\u56fe\u7247\u683c\u5f0f\uff0c\u56fe\u5f62\u6587\u4ef6\uff0cHDF5\u7b49)\u4e2d\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\uff0c\n\u5148\u68c0\u67e5\u770b\u770bPython\u662f\u4e0d\u662f\u5df2\u7ecf\u63d0\u4f9b\u4e86\u73b0\u5b58\u7684\u6a21\u5757\u3002\u56e0\u4e3a\u4e0d\u5230\u4e07\u4e0d\u5f97\u5df2\u6ca1\u6709\u5fc5\u8981\u53bb\u91cd\u590d\u9020\u8f6e\u5b50\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}